package com.wsz.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.common.Result;
import com.wsz.system.pojo.Role;
import com.wsz.system.pojo.UserRole;
import com.wsz.system.service.RoleService;
import com.wsz.vo.AssginRoleVo;
import com.wsz.vo.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags="角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result getAll(){
        List<Role> list = roleService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据ID删除角色")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        boolean b = roleService.removeById(id);
        if(b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("条件分页查询")
    @PostMapping("/find/{page}/{limit}")
    public Result findPageQuery(@PathVariable Long page,
                                @PathVariable Long limit,
                                @RequestBody(required = false) SysRoleQueryVo roleQueryVo){
        Page<Role> page1 = new Page<>(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        String roleName = roleQueryVo.getRoleName();
        if(StringUtils.isNotBlank(roleName)){
            queryWrapper.like("role_name",roleName);
        }
        roleService.page(page1,queryWrapper);
        return Result.ok(page1);
    }


    @ApiOperation("增加角色")
    @PostMapping("/save")
    public Result saveRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if(save) return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("修改角色")
    @PostMapping("/update")
    public Result updateRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        if(b) return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean b = roleService.removeByIds(ids);
        if(b) return Result.ok();
        else return  Result.fail();
    }

    @ApiOperation("通过Id获取角色信息")
    @GetMapping("getRoleInfoById/{id}")
    public Result getRoleInfoById(@PathVariable String id){
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation("获取用户的角色数据")
    @GetMapping("/toAssign/{id}")
    public Result toAssigin(@PathVariable String id){
        Map<String,Object> Roles = roleService.getRolesByUserId(id);
        return Result.ok(Roles);
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("/assignRole")
    public Result assignRole(@RequestBody AssginRoleVo assginRoleVo){

        String userId = assginRoleVo.getUserId();

        //删除原来用户角色
        roleService.removeSysUserRole(userId);

        List<String> roleIdList = assginRoleVo.getRoleIdList();

        List<UserRole> userRoleList = new ArrayList<>();

        for (int i = 0; i < roleIdList.size(); i++) {
            UserRole userRole = new UserRole();

            userRole.setRoleId(roleIdList.get(i));
            userRole.setUserId(userId);

            userRoleList.add(userRole);
        }

        roleService.saveSysUserRole(userRoleList);

        return Result.ok();
    }

}
